local extension = Package("rfenghou_west")
extension.extensionName = "aaa_fenghou"

local U = require "packages/utility/utility"
local RUtil = require "packages/aaa_fenghou/utility/rfenghou_util"

Fk:loadTranslationTable{
  ["rfenghou_west"] = "诸西",
  ["west"] = "西",
}

local function AddWinAudio(general)
  local Win = fk.CreateActiveSkill{ name = general.name.."_win_audio" }
  Win.package = extension
  Fk:addSkill(Win)
end

local haishanggangqinshi = General:new(extension, "rfenghou__1900", "west", 4)
local fanyong = fk.CreateTriggerSkill{
  name = "rfenghou__fanyong",
  anim_type = "defensive",
  frequency = Skill.Compulsory,
  events = {fk.PreCardEffect},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and data.to == player.id and
      (data.card.trueName == "slash" or data.card:isCommonTrick()) and
      not (data.unoffsetable or data.disresponsive or
      table.contains(data.unoffsetableList or {}, player.id) or
      table.contains(data.disresponsiveList or {}, player.id))
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local n = player:getHandcardNum() - 4
    if n > 0 then
      room:askForDiscard(player, n, n, false, self.name, false)
    elseif n < 0 then
      player:drawCards(-n, self.name)
    end
    if player.dead then return end
    local success, dat = room:askForUseActiveSkill(player, "rfenghou__fanyong_active",
      "#rfenghou__fanyong-invoke:::"..data.card:toLogString(), true)
    if success and dat then
      room:recastCard(dat.cards, player, self.name)
      if #dat.cards == 3 then
        return true
      end
    end
    data.unoffsetableList = data.unoffsetableList or {}
    table.insert(data.unoffsetableList, player.id)
  end,
}
local fanyong_active = fk.CreateActiveSkill{
  name = "rfenghou__fanyong_active",
  min_card_num = 1,
  target_num = 0,
  card_filter = function(self, to_select, selected)
    return #selected < 3 and table.every(selected, function (id)
      return Fk:getCardById(id).type ~= Fk:getCardById(to_select).type
    end)
  end,
}
local kuangxiang = fk.CreateTriggerSkill{
  name = "rfenghou__kuangxiang",
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and player.phase == Player.Finish and not player:isKongcheng() and
      table.find(player.room:getOtherPlayers(player), function (p)
        return p:getHandcardNum() == player:getHandcardNum() and
          table.find(player:getCardIds("h"), function (id)
            local card = Fk:cloneCard("slash")
            card:addSubcard(id)
            card.skillName = self.name
            return not player:isProhibited(p, card)
          end)
      end)
  end,
  on_cost = function (self, event, target, player, data)
    local success, dat = player.room:askForUseActiveSkill(player, "rfenghou__kuangxiang_active",
      "#rfenghou__kuangxiang-invoke", true)
    if success and dat then
      self.cost_data = dat
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local card = Fk:cloneCard("slash")
    card:addSubcard(self.cost_data.cards[1])
    card.skillName = self.name
    local use = {
      from = player.id,
      tos = table.map(self.cost_data.targets, function(id) return {id} end),
      card = card,
      extraUse = true,
    }
    room:useCard(use)
    if not player.dead and use.damageDealt and self:triggerable(event, target, player, data) then
      local yes = false
      for _, p in ipairs(room:getAlivePlayers()) do
        if use.damageDealt[p.id] and room:askForSkillInvoke(p, self.name, nil, "#rfenghou__kuangxiang-ask:"..player.id) then
          room:doIndicate(p.id, {player.id})
          yes = true
          break
        end
      end
      if yes then
        room.logic:getCurrentEvent():findParent(GameEvent.SkillEffect, true):addCleaner(function()
          self:doCost(event, target, player, data)
        end)
      end
    end
  end,
}
local kuangxiang_active = fk.CreateActiveSkill{
  name = "rfenghou__kuangxiang_active",
  card_num = 1,
  min_target_num = 1,
  max_target_num = 10,
  card_filter = function(self, to_select, selected)
    if #selected == 0 and table.contains(Self:getCardIds("h"), to_select) then
      local card = Fk:cloneCard("slash")
      card:addSubcard(to_select)
      card.skillName = "rfenghou__kuangxiang"
      return not Self:prohibitUse(card)
    end
  end,
  target_filter = function (self, to_select, selected, selected_cards)
    if #selected_cards == 1 and to_select ~= Self.id then
      local card = Fk:cloneCard("slash")
      card:addSubcard(selected_cards[1])
      card.skillName = "rfenghou__kuangxiang"
      local target = Fk:currentRoom():getPlayerById(to_select)
      return target:getHandcardNum() == Self:getHandcardNum() and not Self:isProhibited(target, card)
    end
  end,
}
Fk:addSkill(fanyong_active)
Fk:addSkill(kuangxiang_active)
haishanggangqinshi:addSkill(fanyong)
haishanggangqinshi:addSkill(kuangxiang)
Fk:loadTranslationTable{
  ["rfenghou__1900"] = "1 9 0 0",
  ["#rfenghou__1900"] = "Freedom",
  ["designer:rfenghou__1900"] = "白驹",

  ["rfenghou__fanyong"] = "翻涌之声",
  [":rfenghou__fanyong"] = "锁定技，当基本牌或普通锦囊牌对你生效前，你抵消此牌的方式改为重铸任意张类型不同的牌、若达到3种类型则视为抵消之；结算前，若你的手牌数不为4，"..
  "你先将手牌数调整至4。",
  ["rfenghou__kuangxiang"] = "狂想涟漪",
  [":rfenghou__kuangxiang"] = "结束阶段，你可以将一张手牌当一张无距离和目标数限制、仅能指定与你手牌数相同的角色为目标的【杀】使用；"..
  "此牌结算后，若有受到此【杀】伤害的角色同意，你可以重复此流程。",
  ["rfenghou__fanyong_active"] = "翻涌之声",
  ["#rfenghou__fanyong-invoke"] = "翻涌之声：请重铸类型不同的牌，若重铸三张则抵消此%arg",
  ["rfenghou__kuangxiang_active"] = "狂想涟漪",
  ["#rfenghou__kuangxiang-invoke"] = "狂想涟漪：你可以将一张手牌当无距离和目标数限制的【杀】使用，只能指定手牌数与你相同的目标",
  ["#rfenghou__kuangxiang-ask"] = "狂想涟漪：是否允许 %src 继续发动“狂想涟漪”？",
}


local beethoven = General:new(extension, "rfenghou__beethoven", "west", 3)
local miejun = fk.CreateTriggerSkill{
  name = "rfenghou__miejun",
  anim_type = "support",
  switch_skill_name = "rfenghou__miejun",
  events = {fk.DamageCaused, fk.DrawNCards},
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(self) or not target then return false end
    local toMark = player:getMark("rfenghou__miejun_to")
    if target.id == toMark or (toMark == 0 and target.seat == 1) then
      return (player:getSwitchSkillState(self.name) == fk.SwitchYang and event == fk.DamageCaused)
          or (player:getSwitchSkillState(self.name) == fk.SwitchYin and event == fk.DrawNCards)
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local prompt
    if event == fk.DamageCaused then
      prompt = "#rfenghou__miejun-damage:"..data.to.id..":"..target.id..":"..data.damage
    else
      prompt = "#rfenghou__miejun-draw::"..target.id..":"..data.n
    end
    if room:askForSkillInvoke(player, self.name, nil, prompt) then
      room:setPlayerMark(player, "@@rfenghou__miejun-turn", 0)
      return true
    else
      room:setPlayerMark(player, "@@rfenghou__miejun-turn", 1)
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.DamageCaused then
      data.damage = data.damage * 2
    else
      data.n = data.n * 2
    end
  end,

  on_acquire = function (self, player)
    player.room:setPlayerMark(player, "@rfenghou__miejun_tar", "seat#1")
  end,
  on_lose = function (self, player)
    player.room:setPlayerMark(player, "@rfenghou__miejun_tar", 0)
    player.room:setPlayerMark(player, "rfenghou__miejun_to", 0)
  end,
}

local rfenghou__miejun_delay = fk.CreateTriggerSkill{
  name = "#rfenghou__miejun_delay",
  mute = true,
  events = {fk.TurnEnd},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(miejun) and player:getMark("@@rfenghou__miejun-turn") ~= 0 and not player:isKongcheng()
    and player:usedSkillTimes(miejun.name, Player.HistoryTurn) == 0
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local orig = player:getMark("rfenghou__miejun_to")
    if orig == 0 then
      orig = table.find(room.players, function (p) return p.seat == 1 end).id
    end
    local targets = table.map(room.alive_players, Util.IdMapper)
    table.removeOne(targets, orig)
    local tos, cid = room:askForChooseCardAndPlayers(player, targets, 1, 1, "jink", "#rfenghou__miejun-recast", miejun.name, true)
    if #tos > 0 then
      self.cost_data = {tos = tos, cards = {cid}}
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(self.cost_data.tos[1])
    room:setPlayerMark(player, "rfenghou__miejun_to", to.id)
    room:setPlayerMark(player, "@rfenghou__miejun_tar", to.general)
    room:recastCard(self.cost_data.cards, player, miejun.name)
  end,
}
miejun:addRelatedSkill(rfenghou__miejun_delay)

beethoven:addSkill(miejun)


local rfenghou__yiku = fk.CreateTriggerSkill{
  name = "rfenghou__yiku",
  anim_type = "control",
  events = {fk.DrawNCards},
  frequency = Skill.Compulsory,
  priority = 0.99, -- 晚于1技能
  can_trigger = function(self, event, target, player, data)
    return target == player and not target:insideExtraTurn() and player:hasSkill(self)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local x = data.n - 1
    data.n = 1
    if x < 0 then return end
    local extraCheck
    for i = 1, x do
      local choices = {}
      if #room:canMoveCardInBoard() > 0 then
        table.insert(choices, "#rfenghou__yiku-move")
      end
      local throwTars = table.filter(room.alive_players, function (p) return not p:isNude() end)
      if #throwTars > 0 then
        table.insert(choices, "#rfenghou__yiku-throw")
      end
      if #choices == 0 then break end
      table.insert(choices, "Cancel")
      local choice = room:askForChoice(player, choices, self.name, "#rfenghou__yiku-choice:::"..i..":"..x)
      if choice == "Cancel" then
        break
      elseif choice == "#rfenghou__yiku-move" then
        local tars = room:askForChooseToMoveCardInBoard(player, choice, self.name, false)
        if #tars == 2 then
          room:askForMoveCardInBoard(player, room:getPlayerById(tars[1]), room:getPlayerById(tars[2]), self.name)
        end
      else
        local tos = room:askForChoosePlayers(player, table.map(throwTars, Util.IdMapper), 1, 1, choice, self.name, false)
        local to = room:getPlayerById(tos[1])
        local cid = room:askForCardChosen(player, to, "he", self.name)
        -- 仅选择弃置装备区牌的情况下检测是否进行额外回合
        if room:getCardArea(cid) == Player.Equip then
          extraCheck = true
        end
        room:throwCard(cid, self.name, to, player)
      end
      if player.dead then return end
    end
    if not extraCheck then return end
    local red, black = 0, 0
    for _, p in ipairs(room.alive_players) do
      for _, id in ipairs(p:getCardIds("ej")) do
        if Fk:getCardById(id).color == Card.Red then
          red = red + 1
        elseif Fk:getCardById(id).color == Card.Black then
          black = black + 1
        end
      end
    end
    if red == black then
      room:setPlayerMark(player, "@@rfenghou__yiku-turn", 1)
      player:gainAnExtraTurn(true, self.name)
    end
  end,
}
beethoven:addSkill(rfenghou__yiku)

Fk:loadTranslationTable{
  ["rfenghou__beethoven"] = "贝多芬",
  ["#rfenghou__beethoven"] = "悲著雄音",
  ["designer:rfenghou__beethoven"] = "拼音",
  ["illustrator:rfenghou__beethoven"] = "Frans Hals", -- 《贝多芬的终夜》

  ["rfenghou__miejun"] = "蔑君",
  [":rfenghou__miejun"] = "转换技，当（一号位）{阳：造成伤害时；阴：于摸牌阶段额定摸牌时}，你可令伤害值或摸牌数的数值翻倍；你能发动但未发动此技能的回合结束时，你可重铸一张【闪】并将另一名角色替换前文（）内。",
  ["@rfenghou__miejun_tar"] = "蔑君",
  ["@@rfenghou__miejun-turn"] = "蔑君:未发动",
  ["#rfenghou__miejun-draw"] = "蔑君：你可以令 %dest 摸牌数 ( %arg ) 翻倍",
  ["#rfenghou__miejun-damage"] = "蔑君：你可以令 %dest 对 %src 造成伤害数 ( %arg ) 翻倍",
  ["#rfenghou__miejun-recast"] = "蔑君：你可以重铸一张【闪】并令一名角色成为“蔑君”的发动目标",
  ["#rfenghou__miejun_delay"] = "蔑君",

  ["rfenghou__yiku"] = "易苦",
  [":rfenghou__yiku"] = "锁定技，你额定回合的摸牌阶段额定摸牌数改为一张；你因此每少摸一张牌，便可弃置一名角色的一张牌或移动场上一张牌，然后场上红黑牌的数量因此相等，当前回合结束时你执行一个额外回合。",
  ["@@rfenghou__yiku-turn"] = "易苦:成功",
  ["#rfenghou__yiku-throw"] = "弃置1名角色1张牌",
  ["#rfenghou__yiku-move"] = "移动场上一张牌",
  ["#rfenghou__yiku-choice"] = "易苦：请选择一项执行（当前第 %arg 次，共 %arg2 次）",
}

local ramses = General:new(extension, "rfenghou__ramses_second", "west", 4)

local rfenghou__duhuang = fk.CreateTriggerSkill{
  name = "rfenghou__duhuang",
  events = {fk.AfterCardsMove},
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) and (player.room:getBanner("RoundCount") or 0) <= player:getMark("@rfenghou__duhuang_hand")
      and player:getMark("@@rfenghou__duhuang_limit") == 0 then
      return (data.extra_data or {}).rfenghou__duhuang_check
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:setBanner("@rfenghou__duhuang_damage", (room:getBanner("@rfenghou__duhuang_damage") or 0) + 1)
  end,

  refresh_events = {fk.AfterCardsMove, fk.EventLoseSkill, fk.EventAcquireSkill, fk.GameStart},
  can_refresh = function (self, event, target, player, data)
    if event == fk.AfterCardsMove then
      if player:hasSkill(self, true) then
        for _, move in ipairs(data) do
          if move.toArea == Player.Hand then return true end
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Player.Hand then
              return true
            end
          end
        end
      end
    elseif event == fk.GameStart then
      return player:hasSkill(self, true)
    else
      return data == self and target == player
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    local num = 0
    local n = player:getHandcardNum()
    if player:hasSkill("rfenghou__xujie") then
      n = n + #table.filter(room.alive_players, function (p)
        return p.kingdom == "west"
      end)
    end
    if event == fk.AfterCardsMove then
      num = math.max(player:getMark("@rfenghou__duhuang_hand"), n)
    elseif event == fk.EventAcquireSkill or event == fk.GameStart then
      num = n
    end
    room:setPlayerMark(player, "@rfenghou__duhuang_hand", num)
    local oldCheck = player.tag["rfenghou__duhuang_ismax"]
    local newCheck = table.every(room:getOtherPlayers(player), function (p)
      return n > p:getHandcardNum()
    end)
    player.tag["rfenghou__duhuang_ismax"] = newCheck
    if newCheck and not oldCheck and event == fk.AfterCardsMove then
      data.extra_data = data.extra_data or {}
      data.extra_data.rfenghou__duhuang_check = true
    end
  end,
}

local rfenghou__duhuang_delay = fk.CreateTriggerSkill{
  name = "#rfenghou__duhuang_delay",
  mute = true,
  events = {fk.DamageCaused, fk.TurnEnd},
  can_trigger = function(self, event, target, player, data)
    if event == fk.DamageCaused then
      return player == target and player.room:getBanner("@rfenghou__duhuang_damage")
    else
      return not player.dead and player:usedSkillTimes("rfenghou__duhuang", Player.HistoryTurn) > 0
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    if event == fk.DamageCaused then
      data.damage = data.damage + room:getBanner("@rfenghou__duhuang_damage")
    else
      local choices = {"#rfenghou__duhuang-play", "#rfenghou__duhuang-turnover"}
      if room:askForChoice(player, choices, self.name) == choices[1] then
        player:gainAnExtraTurn(true, self.name, {phase_table = {Player.Play}})
      else
        room:setPlayerMark(player, "@@rfenghou__duhuang_limit", 2)
        for _, p in ipairs(room:getOtherPlayers(player)) do
          if not p.dead then
            p:turnOver()
          end
        end
      end
    end
  end,

  refresh_events = {fk.AfterTurnEnd, fk.AfterRoundEnd},
  can_refresh = function (self, event, target, player, data)
    if event == fk.AfterTurnEnd then
      return target == player and player.room:getBanner("@rfenghou__duhuang_damage")
    else
      return player:getMark("@@rfenghou__duhuang_limit") > 0
    end
  end,
  on_refresh = function (self, event, target, player, data)
    if event == fk.AfterTurnEnd then
      player.room:setBanner("@rfenghou__duhuang_damage", 0)
    else
      player.room:removePlayerMark(player, "@@rfenghou__duhuang_limit")
    end
  end,
}
rfenghou__duhuang:addRelatedSkill(rfenghou__duhuang_delay)

ramses:addSkill(rfenghou__duhuang)

local rfenghou__songwo = fk.CreateTriggerSkill{
  name = "rfenghou__songwo",
  anim_type = "drawcard",
  events = {fk.Death},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self)
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if not player:isKongcheng() and room:askForSkillInvoke(player, self.name, nil, "#rfenghou__songwo-remove:"..target.id) then
      player:addToPile(self.name, player:getCardIds("h"), false, self.name)
      room:sendLog{
        type = "#rfenghou__changeDeathSource", toast = true,
        from = player.id, to = {target.id}, arg = self.name,
      }
      if data.damage then
        data.damage.from = player
      else
        -- 危险的操作
        data.damage = {from = player, to = target, damage = 1}
      end
    end
    local n = 0
    for _, p in ipairs(room:getOtherPlayers(player)) do
      if player.dead then break end
      if not p.dead and room:askForSkillInvoke(p, self.name, nil, "#rfenghou__songwo-draw:"..player.id) then
        room:doIndicate(p.id, {player.id})
        n = n + 1
        player:drawCards(1, self.name)
      end
    end
    if n >= 3 then
      data.extra_data = data.extra_data or {}
      data.extra_data.skip_reward_punish = true -- 孟达巧思
      room:sendLog{
        type = "#rfenghou__skipDeathProcess", toast = true,
        from = player.id, to = {target.id}, arg = self.name,
      }
    end
  end,
}

local rfenghou__songwo_delay = fk.CreateTriggerSkill{
  name = "#rfenghou__songwo_delay",
  mute = true,
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return target == player and player.phase == Player.Play and #player:getPile("rfenghou__songwo") > 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    player.room:obtainCard(player, player:getPile("rfenghou__songwo"), false, fk.ReasonJustMove, player.id, "rfenghou__songwo")
  end,
}
rfenghou__songwo:addRelatedSkill(rfenghou__songwo_delay)

local xujie = fk.CreateTriggerSkill{
  name = "rfenghou__xujie$",
  frequency = Skill.Compulsory,
}
ramses:addSkill(rfenghou__songwo)
ramses:addSkill(xujie)

Fk:loadTranslationTable{
  ["rfenghou__ramses_second"] = "奥斯曼狄斯", -- 拉美西斯二世
  ["#rfenghou__ramses_second"] = "梅利阿蒙",
  ["designer:rfenghou__ramses_second"] = "Weiß折翼",
  ["illustrator:rfenghou__ramses_second"] = "Wojciech Ostrycharz",

  ["rfenghou__duhuang"] = "睹煌",
  [":rfenghou__duhuang"] = "前X轮内（X为你本局游戏中手牌数的最大值），当你手牌数变为场上唯一最多时，可令当前回合的伤害均+1，并于回合结束后选一项：1.执行一个仅有出牌阶段的回合，2.令所有其他角色翻面，无法发动此技能直到下轮结束。",
  ["#rfenghou__duhuang_delay"] = "睹煌",
  ["@rfenghou__duhuang_damage"] = "睹煌:伤害+",
  ["@rfenghou__duhuang_hand"] = "睹煌:手牌",
  ["#rfenghou__duhuang-play"] = "执行出牌阶段",
  ["#rfenghou__duhuang-turnover"] = "令其他角色翻面",
  ["@@rfenghou__duhuang_limit"] = "睹煌:禁用",

  ["rfenghou__songwo"] = "颂我",
  [":rfenghou__songwo"] = "当一名角色死亡时，其他角色依次可令你摸一张牌，若本次不少于三张，跳过本次死亡奖惩。你可以先移出所有手牌直到下个出牌阶段开始，令你视为伤害来源。",
  ["#rfenghou__songwo_delay"] = "颂我",
  ["#rfenghou__songwo-draw"] = "颂我：你可以令 %src 摸1张牌",
  ["#rfenghou__songwo-remove"] = "颂我：你可以移出所有手牌直到下个出牌阶段开始，令你视为杀死%src 的伤害来源",
  ["#rfenghou__skipDeathProcess"] = "%from 发动了 %arg，跳过了 %to 的死亡奖惩",
  ["#rfenghou__changeDeathSource"] = "%from 发动了 %arg，视为杀死 %to 的伤害来源",

  ["rfenghou__xujie"] = "续阶",
  [":rfenghou__xujie"] = "主公技，锁定技，你的手牌数视为+X（X为场上西势力角色数）。",
}


local kennedy = General:new(extension, "rfenghou__kennedy", "west", 4)

local guowen = fk.CreateActiveSkill{
  name = "rfenghou__guowen",
  anim_type = "drawcard",
  card_num = 0,
  target_num = 0,
  card_filter = Util.FalseFunc,
  prompt = "#rfenghou__guowen",
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0 and not player:isKongcheng()
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local cards = player:getCardIds("h")
    player:showCards(cards)
    cards = table.filter(cards, function(id) return not Fk:getCardById(id).is_damage_card end)
    if #cards > 0 then
      room:recastCard(cards, player, self.name)
    end
  end,
}

local rfenghou__guowen_trigger = fk.CreateTriggerSkill{
  name = "#rfenghou__guowen_trigger",
  refresh_events = {fk.Damage},
  can_refresh = function(self, event, target, player, data)
    return target == player and player:hasSkill(guowen) and player:usedSkillTimes(guowen.name, Player.HistoryPhase) > 0
  end,
  on_refresh = function(self, event, target, player, data)
    player:setSkillUseHistory(guowen.name, 0, Player.HistoryPhase)
  end,
}
guowen:addRelatedSkill(rfenghou__guowen_trigger)

kennedy:addSkill(guowen)

local jiawen = fk.CreateTriggerSkill{
  name = "rfenghou__jiawen",
  events = {fk.EventPhaseStart},
  anim_type = "drawcard",
  can_trigger = function (self, event, target, player, data)
    return target ~= player and target.phase == Player.Play and player:hasSkill(self) and not target:isKongcheng()
    and player:usedSkillTimes(self.name, Player.HistoryRound) == 0
  end,
  on_cost = function (self, event, target, player, data)
    if player.room:askForSkillInvoke(player, self.name, nil, "#rfenghou__jiawen-invoke:"..target.id) then
      self.cost_data = {tos = {target.id}}
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local cards = target:getCardIds("h")
    target:showCards(cards)
    cards = table.filter(cards, function(id) return Fk:getCardById(id).is_damage_card end)
    if #cards > 0 then
      player.room:recastCard(cards, target, self.name)
    end
  end,

  refresh_events = {fk.Damaged},
  can_refresh = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and player:usedSkillTimes(self.name, Player.HistoryRound) > 0
  end,
  on_refresh = function(self, event, target, player, data)
    player:setSkillUseHistory(self.name, 0, Player.HistoryRound)
  end,
}
kennedy:addSkill(jiawen)

Fk:loadTranslationTable{
  ["rfenghou__kennedy"] = "肯尼迪", -- John F. Kennedy
  ["#rfenghou__kennedy"] = "Camelot Era",
  ["designer:rfenghou__kennedy"] = "白帝皇龙甲",
  ["illustrator:rfenghou__kennedy"] = "肯尼迪",
  ["cv:rfenghou__kennedy"] = "肯尼迪",

  ["rfenghou__guowen"] = "国问",
  [":rfenghou__guowen"] = "出牌阶段限一次，你可以展示所有手牌并重铸其中的非伤害类牌。你造成伤害后，重置此技能。",
  ["#rfenghou__guowen"] = "国问：你可展示所有手牌并重铸其中的非伤害类牌",
  ["#rfenghou__guowen_trigger"] = "国问",

  ["rfenghou__jiawen"] = "家问",
  [":rfenghou__jiawen"] = "每轮限一次，其他角色的出牌阶段开始时，你可以展示其所有手牌并重铸其中的伤害类牌。你受到伤害后，重置此技能。",
  ["#rfenghou__jiawen-invoke"] = "家问：你可以展示 %src 所有手牌，重铸其中的伤害类牌！",

  ["$rfenghou__guowen"] = "We choose to go to then moon, we choose to go to then moon!",
  ["$rfenghou__jiawen"] = "Free to choose their own leaders, free to select their own system, free to own their own land,"..
  "free to speak and write and worship, without fear.",
}


local nero = General:new(extension, "rfenghou__nero", "west", 6)

local elong = fk.CreateTriggerSkill{
  name = "rfenghou__meimo",
  anim_type = "drawcard",
  events = {fk.DrawNCards, fk.TurnStart},
  can_trigger = function (self, event, target, player, data)
    if not player:hasSkill(self) then return false end
    if event == fk.DrawNCards then
      return target == player and table.contains(player:getTableMark("@rfenghou__meimo"), 2)
    else
      return target ~= player and #player:getTableMark("@rfenghou__meimo") > 0
      and target:canUseTo(Fk:cloneCard("duel"), player)
    end
  end,
  on_cost = function (self, event, target, player, data)
    return event == fk.DrawNCards or player.room:askForSkillInvoke(target, self.name, nil, "#rfenghou__meimo-duel:"..player.id)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.DrawNCards then
      data.n = data.n + 2
    else
      local choices = table.map(player:getTableMark("@rfenghou__meimo"), function(e) return tostring(e) end)
      local choice = tonumber(room:askForChoice(target, choices, self.name, "#rfenghou__meimo-damage"))
      local card = Fk:cloneCard("duel")
      card.skillName = self.name
      local use = { from = target.id, tos = {{player.id}}, card = card, additionalDamage = choice > 1 and choice - 1 or nil }
      room:useCard (use)
      if use.damageDealt and use.damageDealt[player.id] then
        room:removeTableMark(player, "@rfenghou__meimo", choice)
      end
    end
  end,

  refresh_events = {fk.EventAcquireSkill, fk.EventLoseSkill},
  can_refresh = function (self, event, target, player, data)
    return target == player and data == self
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:setPlayerMark(player, "@rfenghou__meimo", event == fk.EventLoseSkill and 0 or {1,2,3})
  end,
}

local elong_targetmod = fk.CreateTargetModSkill{
  name = "#rfenghou__meimo_targetmod",
  residue_func = function(self, player, skill, scope)
    if skill.trueName == "slash_skill" and scope == Player.HistoryPhase then
      if player:hasSkill(elong) and table.contains(player:getTableMark("@rfenghou__meimo"), 1) then
        return 1
      end
    end
  end,
}
elong:addRelatedSkill(elong_targetmod)


local elong_attackrange = fk.CreateAttackRangeSkill{
  name = "#rfenghou__meimo_attackrange",
  correct_func = function (self, player)
    if player:hasSkill(elong) and table.contains(player:getTableMark("@rfenghou__meimo"), 3) then
      return 3
    end
  end,
}
elong:addRelatedSkill(elong_attackrange)

nero:addSkill(elong)


local baojun = fk.CreateActiveSkill{
  name = "rfenghou__tanghuang",
  anim_type = "offensive",
  card_num = 0,
  target_num = 0,
  prompt = "#rfenghou__tanghuang",
  card_filter = Util.FalseFunc,
  can_use = function(self, player)
    return player:getMark("@@rfenghou__tanghuang") == 0 and not player:isKongcheng()
    and player:canUse(Fk:cloneCard("slash"))
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    room:setPlayerMark(player, "@@rfenghou__tanghuang", 1)
    player:filterHandcards()
  end,
}

local baojun_trigger = fk.CreateTriggerSkill{
  name = "#rfenghou__tanghuang_trigger",
  anim_type = "offensive",
  events = { fk.AskForCardResponse, fk.AfterAskForCardUse, fk.BuryVictim },
  can_trigger = function (self, event, target, player, data)
    if event == fk.BuryVictim then
      return player:hasSkill(baojun) and player:getMark("@@rfenghou__tanghuang") ~= 0
    else
      if target == player and player:hasSkill(baojun) and player:getMark("@@rfenghou__tanghuang") == 0 and not player:isKongcheng() then
        return (data.cardName and data.cardName == "slash") or
        (data.pattern and Exppattern:Parse(data.pattern):matchExp("slash|0|nosuit|none"))
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    return event == fk.BuryVictim or player.room:askForSkillInvoke(player, baojun.name, nil, "#rfenghou__tanghuang")
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.BuryVictim then
      room:setPlayerMark(player, "@@rfenghou__tanghuang", 0)
      player:filterHandcards()
      -- 按身份局执行奖惩……
      if target.role == "rebel" then
        player:drawCards(3, "kill")
      elseif target.role == "loyalist" then
        player:throwAllCards("he")
      end
    else
      room:setPlayerMark(player, "@@rfenghou__tanghuang", 1)
      player:filterHandcards()
    end
  end,
}
baojun:addRelatedSkill(baojun_trigger)

local baojun_filter = fk.CreateFilterSkill{
  name = "#rfenghou__tanghuang_filter",
  card_filter = function(self, card, player)
    return player:getMark("@@rfenghou__tanghuang") ~= 0 and table.contains(player.player_cards[Player.Hand], card.id)
  end,
  view_as = function(self, card)
    local c = Fk:cloneCard("fire__slash", card.suit, card.number)
    c.skillName = "rfenghou__tanghuang"
    return c
  end,
}
baojun:addRelatedSkill(baojun_filter)

nero:addSkill(baojun)

Fk:loadTranslationTable{
  ["rfenghou__nero"] = "尼禄",-- Nero Claudius Caesar
  ["#rfenghou__nero"] = "",
  ["designer:rfenghou__nero"] = "白驹",
  ["illustrator:rfenghou__nero"] = "佚名",
  ["cv:rfenghou__nero"] = "Bruno Prevedi",

  ["rfenghou__meimo"] = "殙殁",
  [":rfenghou__meimo"] = "你的出【杀】次数上限+1，额定摸牌数+2，攻击范围+3。其他角色于回合开始时可以视为对你使用伤害基数为以上一个数值的【决斗】，对你造成伤害后，移除该数值对应句。",
  ["#rfenghou__meimo-duel"] = "殙殁：你可以视为对 %src 使用一张【决斗】！",
  ["#rfenghou__meimo-damage"] = "殙殁：选择【决斗】的伤害基数！",
  ["@rfenghou__meimo"] = "殙殁",

  ["rfenghou__tanghuang"] = "煻煌",
  [":rfenghou__tanghuang"] = "你需要使用或打出【杀】时，可以令你的所有手牌均视为火【杀】至有角色死亡，然后你须执行主公杀死其的奖惩。",
  ["#rfenghou__tanghuang_filter"] = "煻煌",
  ["#rfenghou__tanghuang_trigger"] = "煻煌",
  ["@@rfenghou__tanghuang"] = "煻煌",
  ["#rfenghou__tanghuang"] = "煻煌：你可以令所有手牌视为火【杀】！",

  ["$rfenghou__meimo1"] = "Da ogni parte m'assalgono!（他们从四面八方涌向我！）",
  ["$rfenghou__meimo2"] = "Ah！Chi mi masconde？（啊！谁能将我庇护！）",
  ["$rfenghou__tanghuang1"] = "Rinasco libero e forte！（我已重生，自由而强大！）",
  ["$rfenghou__tanghuang2"] = "Cantano i versi miei.（他们在吟唱我的诗篇！）",
  ["$rfenghou__tanghuang3"] = "No！Fortuna in fronte！（不，好运尚在前方！）",
}

local jett = General:new(extension, "rfenghou__jett", "ghan", 4, 4, General.Female)
local zhufeng = fk.CreateTriggerSkill{
  name = "rfenghou__zhufeng",
  events = {fk.TurnStart, fk.CardUsing, fk.CardUseFinished},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) and target == player then
      if event == fk.TurnStart then
        return true
      elseif event == fk.CardUsing then
        return player:getMark("@rfenghou__zhufeng-turn") ~= 0 and player:getMark("rfenghou__zhufeng_count-turn") < 3
      else
        return player:getMark("@rfenghou__zhufeng-turn") ~= 0 and player:getMark("rfenghou__zhufeng_count-turn") == 3
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if event == fk.TurnStart then
      return room:askForSkillInvoke(player, self.name)
    elseif event == fk.CardUsing then
      room:addPlayerMark(player, "rfenghou__zhufeng_count-turn", 1)
      local index = player:getMark("rfenghou__zhufeng_count-turn")
      local good = player:getMark("rfenghou__zhufeng_fail-turn") == 0 and player:getMark("@rfenghou__zhufeng-turn")
      [index] == data.card:getTypeString().."_char"
      local mark = player:getMark("@rfenghou__zhufeng-turn")
      mark[index] = good and "√" or "x"
      room:setPlayerMark(player, "@rfenghou__zhufeng-turn", mark)
      if good then
        return true
      else
        room:setPlayerMark(player, "rfenghou__zhufeng_fail-turn", 1)
      end
    else
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.TurnStart then
      local all_choices = {"basic", "trick", "equip"}
      local choices, chosen = table.simpleClone(all_choices), {}
      for i = 1, 3 do
        local ch = room:askForChoice(player, choices, self.name, "#rfenghou__zhufeng-choice:::"..i, false, all_choices)
        table.insert(chosen, ch.."_char")
        table.removeOne(choices, ch)
      end
      room:setPlayerMark(player, "@rfenghou__zhufeng-turn", chosen)
    elseif event == fk.CardUsing then
      player:drawCards(1, self.name)
    else
      room:setPlayerMark(player, "@rfenghou__zhufeng-turn", 0)
      --[[local choices = {"#rfenghou__zhufeng_sort", "#rfenghou__zhufeng_damage"}
      if player:getMark("rfenghou__zhufeng_fail-turn") > 0 then
        choices = {room:askForChoice(player, choices, self.name)}
      end
      for _, ch in ipairs(choices) do
        if ch == "#rfenghou__zhufeng_sort" then]]--
          if room:askForSkillInvoke(player, self.name, nil, "#rfenghou__zhufeng-sort") then
            room:setPlayerMark(player, "rfenghou__zhufeng_fail-turn", 0)
            room:setPlayerMark(player, "rfenghou__zhufeng_count-turn", 0)
            self:use(fk.TurnStart, player, player, {})
         end
         --[[else
          local tos = player.room:askForChoosePlayers(player, table.map(player.room.alive_players, Util.IdMapper), 1, 1,
          "#rfenghou__zhufeng-choose", self.name, true)
          if #tos > 0 then
            room:damage{ to = room:getPlayerById(tos[1]), from = player, damage = 1, skillName = self.name }
          end
        end
      end]]--
    end
  end,
}
jett:addSkill(zhufeng)


local juren = fk.CreateActiveSkill{
  name = "rfenghou__juren",
  anim_type = "offensive",
  card_num = 0,
  target_num = 0,
  prompt = "#rfenghou__juren",
  card_filter = Util.FalseFunc,
  can_use = function(self, player)
    return player:getMark("@@rfenghou_readying:::"..self.name) == 0
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    room:handleAddLoseSkills(player, "rfenghou__juren_viewas&")
    room.logic:getCurrentEvent():findParent(GameEvent.Turn):addCleaner(function()
      room:handleAddLoseSkills(player, "-rfenghou__juren_viewas&")
    end)
  end,
}
local juren_trigger = fk.CreateTriggerSkill{
  name = "#rfenghou__juren_trigger",
  anim_type = "drawcard",
  events = {fk.Deathed},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(juren) and data.damage and data.damage.from == player and #player.room:getBanner("@$CenterArea") > 0
  end,
  on_cost = Util.TrueFunc,
  on_use= function (self, event, target, player, data)
    local room = player.room
    local x = #table.filter(room.players, function(p) return p.dead end)
    local cards = room:getBanner("@$CenterArea")
    x = math.min(x, #cards)
    cards = U.askforChooseCardsAndChoice(player, cards, {"OK"}, "rfenghou__juren", "#rfenghou__juren-get:::"..x)
    room:obtainCard(player, cards, true, fk.ReasonJustMove, player.id, juren.name)
  end,
}
juren:addRelatedSkill(juren_trigger)

local juren_prohibit = fk.CreateProhibitSkill{
  name = "#rfenghou__juren_prohibit",
  prohibit_use = function(self, player, card)
    return player:usedSkillTimes("rfenghou__juren", Player.HistoryTurn) > 0 and
      table.find(Fk:currentRoom():getBanner("@$CenterArea"), function (id)
        return card:compareSuitWith(Fk:getCardById(id))
      end)
  end,
}
juren:addRelatedSkill(juren_prohibit)

juren.RfenghouReadySkill = true
juren.CenterArea = true

--jett:addSkill(juren)

local juren_viewas = fk.CreateViewAsSkill{
  name = "rfenghou__juren_viewas&",
  anim_type = "offensive",
  prompt = "#rfenghou__juren_viewas",
  pattern = "unexpectation",
  card_filter = function(self, to_select, selected)
    return #selected == 0 and table.contains(Self:getHandlyIds(true), to_select)
    -- 为了游戏体验，小耦一下
    and not table.find(Fk:currentRoom():getBanner("@$CenterArea"), function (id)
      return Fk:getCardById(to_select):compareSuitWith(Fk:getCardById(id))
    end)
  end,
  view_as = function(self, cards)
    if #cards ~= 1 then return nil end
    local c = Fk:cloneCard("unexpectation")
    c.skillName = juren.name
    c:addSubcard(cards[1])
    return c
  end,
  enabled_at_play = Util.TrueFunc,
  enabled_at_response = function (self, player, response)
    return not response
  end,
}
Fk:addSkill(juren_viewas)

Fk:loadTranslationTable{
  ["rfenghou__jett"] = "韩善宇", -- 捷风
  ["#rfenghou__jett"] = "镜影刀锋",
  ["designer:rfenghou__jett"] = "花俏鬘娇",
  ["illustrator:rfenghou__jett"] = "Kiko.L",

  ["rfenghou__zhufeng"] = "逐风",
  [":rfenghou__zhufeng"] = "回合开始时，你可以指定你本回合下三张牌的类型顺序（不得相同）。当你使用牌时，若符合你指定的顺序，你摸一张牌。若如此做，"..
  "当你使用三张牌后，你可以重新如此指定顺序。",
  ["#rfenghou__zhufeng-choice"] = "为“逐风”声明顺序：当前第 %arg 个",
  ["@rfenghou__zhufeng-turn"] = "逐风",
  ["#rfenghou__zhufeng-sort"] = "你可以为“逐风”重新声明顺序",
  ["#rfenghou__zhufeng_sort"] = "重新声明顺序",
  ["#rfenghou__zhufeng_damage"] = "分配1点伤害",
  ["#rfenghou__zhufeng-choose"] = "逐风：对一名角色造成1点伤害",

  ["rfenghou__juren"] = "飓刃",
  [":rfenghou__juren"] = "<a href='rfenghou_ready_skill'>蓄势技</a>，出牌阶段，你可以令本回合：你的手牌均可当【出其不意】使用；"..
  "你不能使用中央区已有花色的牌；当你杀死一名角色后，获得中央区的X张牌（X为已死亡角色数）。",
  ["#rfenghou__juren"] = "飓刃：令你本回合可以用手牌转化【出其不意】，且不能使用中央区已有花色的牌！",
  ["#rfenghou__juren_trigger"] = "飓刃",
  ["rfenghou__juren_viewas&"] = "飓刃",
  [":rfenghou__juren_viewas&"] = "本回合中，你可以将一张手牌当【出其不意】使用。",
  ["#rfenghou__juren_viewas"] = "将一张手牌当【出其不意】使用",
  ["@rfenghou__juren-turn"] = "飓刃",
  ["#rfenghou__juren-get"] = "飓刃：获得中央区的 %arg 张牌！",
  ["@@rfenghou_readying:::rfenghou__juren"] = "飓刃失效",

  ["$rfenghou__juren1"] = "",
  ["$rfenghou__juren2"] = "",
}




local kaisa = General:new(extension, "rfenghou__kaisa", "west", 4, 4, General.Female)
local lieyu = fk.CreateTriggerSkill{
  name = "rfenghou__lieyu",
  anim_type = "offensive",
  frequency = Skill.Compulsory,
  events = {fk.DamageCaused},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and data.to ~= player and not player:inMyAttackRange(data.to)
    and not player:isNude()
  end,
  on_cost = function (self, event, target, player, data)
    local cards = player.room:askForDiscard(player, 1, 1, true, self.name, true, ".", "#rfenghou__lieyu-cost:"..data.to.id, true)
    if #cards > 0 then
      self.cost_data = {cards = cards, tos = {data.to.id}}
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    player.room:throwCard(self.cost_data.cards, self.name, player, player)
    data.damage = data.damage + 1
  end,
}
local lieyu_targetmod = fk.CreateTargetModSkill{
  name = "#rfenghou__lieyu_targetmod",
  frequency = Skill.Compulsory,
  main_skill = lieyu,
  bypass_distances = function (self, player, skill, card, to)
    return player:hasSkill(lieyu) and card and card.is_damage_card
  end,
  --[[extra_target_func = function(self, player, skill, card)
    if player:hasSkill(lieyu) and card and (card.type == Card.TypeBasic or (card:isCommonTrick() and card.name ~= "collateral")) then
      return 1
    end
  end,]]--
}
local yanzhu = fk.CreateTriggerSkill{
  name = "rfenghou__yanzhu",
  events = {fk.EventPhaseEnd},
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and player:distanceTo(target) == 1 and not target.dead and target.phase == Player.Draw
    and not target:isRemoved() and not target:isNude()
  end,
  on_cost = function (self, event, target, player, data)
    self.cost_data = {tos = {target.id}}
    return player.room:askForSkillInvoke(player, self.name, nil, "#rfenghou__yanzhu-invoke::"..target.id)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local choices = {"rfenghou__yanzhu2:"..player.id}
    if table.find(target:getCardIds("he"), function(id) return not target:prohibitDiscard(id) end) then
      table.insert(choices, 1, "rfenghou__yanzhu1")
    end
    local choice = room:askForChoice(target, choices, self.name)
    if choice[17] == "1" then
      room:askForDiscard(target, 1, 1, true, self.name, false)
    else
      local cards = room:askForCard(target, 1, 1, true, self.name, false, ".", "#rfenghou__yanzhu-give:"..player.id)
      room:obtainCard(player, cards, false, fk.ReasonGive, target.id, self.name)
      if table.contains(player:getCardIds("he"), cards[1]) then
        U.askForUseVirtualCard(room, player, "stab__slash", cards, self.name,
        "#rfenghou__yanzhu-slash:::"..Fk:getCardById(cards[1]):toLogString(), true, true, false, true)
      end
    end
  end,
}
lieyu:addRelatedSkill(lieyu_targetmod)
kaisa:addSkill(lieyu)
kaisa:addSkill(yanzhu)
Fk:loadTranslationTable{
  ["rfenghou__kaisa"] = "卡莎",
  ["#rfenghou__kaisa"] = "虚空之女",
  ["designer:rfenghou__kaisa"] = "祭祀",
  ["illustrator:rfenghou__kaisa"] = "潘诚伟",

  ["rfenghou__lieyu"] = "猎雨",
  [":rfenghou__lieyu"] = "锁定技，你使用伤害牌无距离限制；你对攻击范围外的其他角色造成伤害时，你可以弃置一张牌令伤害值+1。",
  ["#rfenghou__lieyu-cost"] = "猎雨：你可以弃置一张牌，对 %src 造成伤害+1",

  ["rfenghou__yanzhu"] = "魇逐",
  [":rfenghou__yanzhu"] = "你距离1的角色摸牌阶段结束时，你可以令其选择一项：1.弃置一张牌；2.交给你一张牌，然后你可以将之当刺【杀】使用。",
  ["#rfenghou__yanzhu-invoke"] = "魇逐：你可以令 %dest 选择弃牌或交给你牌",
  ["rfenghou__yanzhu1"] = "弃置一张牌",
  ["rfenghou__yanzhu2"] = "交给 %src 一张牌",
  ["#rfenghou__yanzhu-slash"] = "魇逐：你可以将 %arg 当刺【杀】使用",
  ["#rfenghou__yanzhu-give"] = "魇逐:交给 %src 一张牌，其可以将之当刺【杀】使用",
}

local zhangxianzhong = General:new(extension, "rfenghou__zhangxianzhong", "west", 4)

local rfenghou__huangwu = fk.CreateTriggerSkill{
  name = "rfenghou__huangwu",
  events = {fk.GameStart, fk.AfterDrawNCards, fk.TurnEnd},
  anim_type = "drawcard",
  derived_piles = "star",
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(self) then return false end
    if event == fk.TurnEnd then
      local mark = player:getMark("rfenghou__huangwu-turn")
      return type(mark) == "string" and #table.filter(player:getPile("star"), function (id)
        return Fk:getCardById(id).trueName == "slash"
      end) > tonumber(mark)
    else
      return event == fk.GameStart or
      (target == player and not player:isKongcheng() and #player:getPile("star") > 0)
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event ~= fk.AfterDrawNCards then return true end
    return player.room:askForSkillInvoke(player, self.name, data, "#rfenghou__huangwu-exchange")
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.TurnEnd then
      local tos = room:askForChoosePlayers(player, table.map(room.alive_players, Util.IdMapper), 1, 1,
      "#rfenghou__huangwu-damage", self.name, false)
      room:damage { from = player, to = room:getPlayerById(tos[1]), damage = 1, skillName = self.name, damageType = fk.FireDamage }
    else
      if event == fk.GameStart then
        player:addToPile("star", room:getNCards(7), true, self.name)
        if player.dead or player:isKongcheng() or #player:getPile("star") == 0 then return false end
      end
      local cids = room:askForArrangeCards(player, self.name,
      {player:getPile("star"), player:getCardIds(Player.Hand), "star", "$Hand"}, "#rfenghou__huangwu-exchange", true)
      U.swapCardsWithPile(player, cids[1], cids[2], self.name, "star", true, player.id)
    end
  end,

  refresh_events = {fk.TurnStart},
  can_refresh = function (self, event, target, player, data)
    return player:hasSkill(self, true)
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:setPlayerMark(player, "rfenghou__huangwu-turn", tostring(#table.filter(player:getPile("star"), function (id)
      return Fk:getCardById(id).trueName == "slash"
    end)))
  end,
}
zhangxianzhong:addSkill(rfenghou__huangwu)

local rfenghou__huabian = fk.CreateTriggerSkill{
  name = "rfenghou__huabian",
  events = {fk.EventPhaseStart, fk.AfterCardsMove},
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(self) then return false end
    if event == fk.EventPhaseStart then
      return target.phase == Player.Play and #player:getPile("star") > 0 and player:usedSkillTimes(self.name, Player.HistoryRound) == 0
    elseif player:usedSkillTimes(self.name) > 0 then
      local e = player.room.logic:getCurrentEvent().parent
      if e and e.event == GameEvent.Death then
        local cards = {}
        for _, move in ipairs(data) do
          if move.moveReason == fk.ReasonDiscard
          and move.from and player.room:getPlayerById(move.from).dead then
            for _, info in ipairs(move.moveInfo) do
              table.insertIfNeed(cards, info.cardId)
            end
          end
        end
        if #cards > 0 then
          self.cost_data = {cards = cards}
          return true
        end
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.AfterCardsMove then return true end
    local use = player.room:askForUseRealCard(player, ".|.|.|star", self.name, "#rfenghou__huabian-invoke:"..target.id,
    {expand_pile = "star", bypass_distances = true}, true, true)
    if use then
      self.cost_data = {tos = {target.id}, skill_info = use}
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart then
      room:useCard(self.cost_data.skill_info)
      if not player.dead and not target.dead then
        local choice = room:askForChoice(player, {"rfenghou__huabian_draw:"..target.id,
        "rfenghou__huabian_discard:"..target.id}, self.name)
        if choice:startsWith("rfenghou__huabian_draw") then
          target:drawCards(1, self.name)
        elseif not target:isNude() then
          room:askForDiscard(target, 1, 1, true, self.name, false)
        end
      end
    else
      player:addToPile("star", self.cost_data.cards, true, self.name)
    end
  end,
}
zhangxianzhong:addSkill(rfenghou__huabian)

if not Fk.skills["qixing"] then
local qixing = fk.CreateTriggerSkill{
  name = "qixing",
  events = {fk.GameStart, fk.AfterDrawNCards},
  anim_type = "drawcard",
  derived_piles = "$star",
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(self) then return false end
    return event == fk.GameStart or
    (target == player and not player:isKongcheng() and #player:getPile("$star") > 0)
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.GameStart then return true
    else return player.room:askForSkillInvoke(player, self.name, data) end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.GameStart then
      player:addToPile("$star", room:getNCards(7), false, self.name)
      if player.dead or player:isKongcheng() or #player:getPile("$star") == 0 then return false end
    end
    local cids = room:askForArrangeCards(player, self.name,
    {player:getPile("$star"), player:getCardIds(Player.Hand), "$star", "$Hand"}, "#qixing-exchange", true)
    U.swapCardsWithPile(player, cids[1], cids[2], self.name, "$star")
  end,
}
end

zhangxianzhong:addRelatedSkill("qixing")
AddWinAudio(zhangxianzhong)

Fk:loadTranslationTable{
  ["rfenghou__zhangxianzhong"] = "张献忠",
  ["#rfenghou__zhangxianzhong"] = "浮命髏川",
  ["designer:rfenghou__zhangxianzhong"] = "拼音",
  ["illustrator:rfenghou__zhangxianzhong"] = "张献忠",
  ["cv:rfenghou__zhangxianzhong"] = "妙啊",

  ["rfenghou__huangwu"] = "荒武",
  [":rfenghou__huangwu"] = "锁定技，你视为拥有“七星”，你的“星”明置。每回合结束时，若“星”中的【杀】比回合开始时多，你对一名角色造成1点火焰伤害。",
  ["#rfenghou__huangwu-exchange"] = "荒武：你可以交换任意张“星”牌与手牌",
  ["#rfenghou__huangwu-damage"] = "荒武：选择一名角色，你对其造成1点火焰伤害",
  ["star"] = "星",

  ["rfenghou__huabian"] = "猾变",
  [":rfenghou__huabian"] = "每轮限一次，一名角色的出牌阶段开始时，你可无视距离使用一张“星”，令其摸或弃一张牌；发动过“猾变”的回合内，你将已死亡角色的弃牌置入“星”中。",
  ["#rfenghou__huabian-invoke"] = "猾变：你可以使用一张“星”，然后令 %src 摸或弃1张牌",
  ["rfenghou__huabian_discard"] = "令 %src 弃1张牌",
  ["rfenghou__huabian_draw"] = "令 %src 摸1张牌",

  ["qixing"] = "七星",
  [":qixing"] = "游戏开始时，你将牌堆顶的七张牌扣置于武将牌上，称为“星”，然后你可以用任意张手牌替换等量的“星”；摸牌阶段结束时，你可以用任意张手牌替换等量的“星”。",
  ["$star"] = "星",
  ["#qixing-exchange"] = "七星：你可以用任意张手牌替换等量的“星”",

  ["$rfenghou__huangwu1"] = "天生万物以养人，人无一物以报天。",
  ["$rfenghou__huangwu2"] = "状元百官都如狗，总是刀下觳觫材。",
  ["$rfenghou__huabian1"] = "天高高，风肃肃。正合杀人、毋封刀匕。",
  ["$rfenghou__huabian2"] = "快哉风，直星斗。今夜行兵，尽洗街市！",
  ["$qixing_rfenghou__zhangxianzhong1"] = "忽有狂徒夜磨刀，帝星飘摇荧惑高！",
  ["$qixing_rfenghou__zhangxianzhong2"] = "人生富贵总由天，草民之穷由天谴。",
  ["~rfenghou__zhangxianzhong"] = "杀，杀，杀！",
  ["$rfenghou__zhangxianzhong_win_audio"] = "天曰可杀，地曰可杀，逆天之人立死跪亦死！",
}


return extension
